%                    Download Airfoil Data
%
%
% Created by:  David Gitz (davidgitz@gmail.com
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Written for: AIAA Student Org
%
%             Do not use without explicit permission from
%             either the author or organization this program
%             was written for. Please cite the author for
%             any code/ideas/thought that was borrowed from
%             this work.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Any Global declarations go here
close all
clear all

dataFileDirArray = cellstr(['http://www.ae.illinois.edu/m-selig/pd/pub/lsat/vol1/';'http://www.ae.illinois.edu/m-selig/pd/pub/lsat/vol2/';'http://www.ae.illinois.edu/m-selig/pd/pub/lsat/vol3/']);
mkdir('DATA\DRAG');
mkdir('DATA\LIFT');
%Download all Data Files
k = 1;
for i = 1:length(dataFileDirArray)
    text = urlread(dataFileDirArray{i});
    linkindex = strfind(text,'HREF="');
    for j = 1:length(linkindex)-1
        tempstr = text(linkindex(j):linkindex(j+1));
        tempstr = tempstr(strfind(tempstr,'HREF="')+6:length(tempstr));
        tempstr =  tempstr(1:(strfind(tempstr,'"')-1));
        indarray = findstr(tempstr,'/');
        tempstr = tempstr(indarray(length(indarray))+1:length(tempstr));
        if (~isempty(tempstr))
            urllist{k} = [dataFileDirArray{i},tempstr];
            k = k+1;
        end
    end
end
drgindex = 1;
lftindex = 1;

%Parse for url path for DRAG/LIFT Files
for i = 1:length(urllist)
    [pathstr, name, ext] = fileparts(urllist{i});
    switch ext
        case '.DRG'
            drgurls{drgindex} = urllist{i};
            drgindex = drgindex + 1;
        case '.LFT'
            lfturls{lftindex} = urllist{i};
            lftindex = lftindex + 1;
        otherwise
    end
end
disp('Downloading Files...');
%Download and Save DRAG Files
for i = 1:length(drgurls)
    [pathstr, name, ext] = fileparts(drgurls{i});
    filename = ['DATA/DRAG/',name,'.txt'];
    urlwrite(drgurls{i},filename);
end
disp('Done Downloading Drag Files.  Now Downloading Lift Files...');
%Download and Save LIFT Files
for i = 1:length(lfturls)
    [pathstr, name, ext] = fileparts(lfturls{i});
    filename = ['DATA/LIFT/',name,'.txt'];
    urlwrite(lfturls{i},filename);
end
disp('Done Downloading all Files.');
close all;
clear all;


